# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 主成分分析PCA.py
# @Author: dongguangwen
# @Date  : 2025-02-13 22:11
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris

# 加载数据集
x, y = load_iris(return_X_y=True)
print(x[:5])
print(y[:5])

# 保留指定比例的信息
transformer = PCA(n_components=0.95)
x_pca = transformer.fit_transform(x)
print(x_pca[:5])

# 保留指定数量特征
transformer2 = PCA(n_components=2)
x_pca2 = transformer2.fit_transform(x)
print(x_pca2[:5])

"""
[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]]
[0 0 0 0 0]
[[-2.68412563  0.31939725]
 [-2.71414169 -0.17700123]
 [-2.88899057 -0.14494943]
 [-2.74534286 -0.31829898]
 [-2.72871654  0.32675451]]
[[-2.68412563  0.31939725]
 [-2.71414169 -0.17700123]
 [-2.88899057 -0.14494943]
 [-2.74534286 -0.31829898]
 [-2.72871654  0.32675451]]
 """
